package com.hntxk.cqzd.diacrisis.service.impl;

import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisFetus;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisFetusDetail;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisInfo;
import com.hntxk.cqzd.diacrisis.model.DiacrisisAllModel;
import com.hntxk.cqzd.diacrisis.model.FetusAndDetailsModel;
import com.hntxk.cqzd.diacrisis.service.DiacrisisFetusDetailService;
import com.hntxk.cqzd.diacrisis.service.DiacrisisFetusService;
import com.hntxk.cqzd.pacs.model.PacsModel;
import com.hntxk.cqzd.pacs.service.PacsInterfaceService;
import com.hntxk.sp.dict.manager.DictManager;
import com.hntxk.sp.dict.model.Dict;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("diacFetusService")
@Transactional(rollbackFor = Exception.class)
public class DiacrisisFetusServiceImpl extends BaseServiceImpl<TblDiacrisisFetus> implements DiacrisisFetusService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiacrisisFetusMapper.";

    private final DiacrisisFetusDetailService diacrisisFetusDetailService;

    @Qualifier("PacesInterfaceServiceImpl")
    @Autowired
    PacsInterfaceService pacsInterfaceService;

    public DiacrisisFetusServiceImpl(@Qualifier("diacFetusDetailService") DiacrisisFetusDetailService diacrisisFetusDetailService) {
        super(MAPPER_NAMESPACE);
        this.diacrisisFetusDetailService = diacrisisFetusDetailService;
    }


    @Override
    public ResultModel<DiacrisisAllModel> insert(DiacrisisAllModel diacrisisAllModel) {
        //获取身份证号
        String patientCardId = diacrisisAllModel.getTblDiacrisisInfo().getPatientCardId();
        //获取outpatientNo
        String outpatientNo = diacrisisAllModel.getTblDiacrisisInfo().getOutpatientNo();
        List<PacsModel> outPatentEquals=new ArrayList<>();
        if(StringUtil.isNotNull(patientCardId)){
            //根据身份证号获取b超检查结果
            /*
            List<PacsModel> pacsModelList= pacsInterfaceService.getPaces(patientCardId, null);
            if (pacsModelList.size()>0) {
                //筛选outpatientNo 相同的结果
                for (int i = 0; i <pacsModelList.size() ; i++) {
                    if(Objects.equals(pacsModelList.get(i).getOutPatientNo(),outpatientNo)){
                        outPatentEquals.add(pacsModelList.get(i));
                    }
                }
            }*/
        }
        List<FetusAndDetailsModel> diacrisisFetus = diacrisisAllModel.getDiacrisisFetus();
        FetusAndDetailsModel fetusAndDetail;
        if(diacrisisFetus.size()>0){
            for (int i = 0; i < diacrisisFetus.size(); i++) {
                fetusAndDetail = diacrisisFetus.get(i);
                getPrimaryKey(fetusAndDetail);
                setSelectName(fetusAndDetail);
                TblDiacrisisInfo tblDiacrisisInfo = diacrisisAllModel.getTblDiacrisisInfo();
                fetusAndDetail.setDiacrisisId(tblDiacrisisInfo.getDiacrisisId());
                //如果详情有值 插入详情
                List<TblDiacrisisFetusDetail> fetusDetail = fetusAndDetail.getFetusDetail();

                insert(MAPPER_NAMESPACE+"insert",fetusAndDetail);

                if(fetusDetail.size()>0){
                    /*
                    PacsModel pacsModel=new PacsModel();
                    //检查结果有数据 插入
                    if (outPatentEquals.size()>0) {
                        pacsModel= outPatentEquals.get(0);
                        outPatentEquals.remove(0);
                    }
                    diacrisisFetusDetailService.insertDetail(fetusAndDetail,pacsModel);*/
                    diacrisisFetusDetailService.insertDetail(fetusAndDetail);
                }

                LogRecordUtil.addLog(new ActionLog(FetusAndDetailsModel.class, fetusAndDetail.getFetusId() + "", "12",
                        ActionType.ADD, "新增B超检查信息：" + fetusAndDetail.getChorionicName() + "[ " + fetusAndDetail.getFetusRemark() + " ]"));

            }
        }

        return ResultModel.empty();
    }

    @Override
    public ResultModel<DiacrisisAllModel> update(DiacrisisAllModel diacrisisAllModel) {

        //获取外键id
        Long diacrisisId = diacrisisAllModel.getTblDiacrisisInfo().getDiacrisisId();

        //查询出数据库中的记录
        List<FetusAndDetailsModel> fetusAndDetailsModels = getByFkId(diacrisisId);

        //删除记录
        if(fetusAndDetailsModels.size()>0){
            // 先删除从表的数据
                    diacrisisFetusDetailService.delete(fetusAndDetailsModels);

            //根据外键删除此表的数据
            delete(MAPPER_NAMESPACE+"deleteByFkId",diacrisisId);
        }
        //
        return insert(diacrisisAllModel);


    }



    @Override
    public List<FetusAndDetailsModel> getByFkId(Long id) {
        TblDiacrisisFetus tblDiacrisisFetus=new TblDiacrisisFetus();
        tblDiacrisisFetus.setDiacrisisId(id);
        List<FetusAndDetailsModel> list = (List<FetusAndDetailsModel>) query(MAPPER_NAMESPACE + "getByFkId", tblDiacrisisFetus);
        List<FetusAndDetailsModel> resultList=new ArrayList<>();
        if(list.size()>0) {
            resultList = diacrisisFetusDetailService.getByFkId(list);
        }
        return resultList;
    }


    /**
     * 设置下拉name值
     * @param fetus
     */
    public void setSelectName(FetusAndDetailsModel fetus){

        Long siteId = fetus.getSiteId();
        if (siteId!=null) {
            //设置胎盘位置下拉name
            Dict baseSite = DictManager.getInstance().getDict("CQ_BASE_SITE", siteId);
            fetus.setSiteName(baseSite.getText());
        }

        Long bAbnormalId = fetus.getBAbnormalId();
        if (bAbnormalId!=null) {
            //设置b超异常下拉值
            Dict bAbnormal = DictManager.getInstance().getDict("CQ_BASE_B_ABNORMAL", bAbnormalId);
            fetus.setBAbnormal(bAbnormal.getText());
        }

        Long chorionicId = fetus.getChorionicId();
        if (chorionicId!=null) {
            Dict baseChorionice = DictManager.getInstance().getDict("CQ_BASE_CHORIONICE_ID", chorionicId);
            fetus.setChorionicName(baseChorionice.getText());
        }


    }


}
